if (window.plus) {
	ready();
} else {
	document.addEventListener('plusready', ready, false);
}

$(function() {
	if (AGENT_CLIENT.WEB) {
		ready();
	}
})

function ready() {

	var user = {};
	var currentLoginUser = {};
	var isSetPassword = false;
	var html = [];
	var thisPage = {};
	var account_safePage = function() {
		this.init();
	}
	account_safePage.prototype = {
		init: function() {
			this.initData();
			this.bindEvent();
		},
		initData: function() {
			document.addEventListener("touchstart", function() {}, true);
			user = getLocalStorage(STORAGE_MANAGER.user);
			if (!user) {
				redirect("../account/login.html");
				return;
			}
			//获取用户信息
			$.ajax({
				headers: {
					"token": user.token
				},
				type: "GET",
				url: CONFIG_MANAGER.RequstUrl + "v1/member/" + user.id,
				async: true
			}).done(function(data) {
				setLocalStorage(STORAGE_MANAGER.currentmember, data);
			});
			//获取用户是否设置支付密码
			$.ajax({
				type: "GET",
				headers: {
					"token": user.token
				},
				url: CONFIG_MANAGER.RequstUrl + "v1/member/safety/paypasswd/" + user.id,
				async: true
			}).done(function(data) {
				setLocalStorage(STORAGE_MANAGER.issetpaypasswd, true);
			}).fail(function(data) {
				setLocalStorage(STORAGE_MANAGER.issetpaypasswd, false);
			});

			// 显示新版本提示
			var latestVersion = getLocalStorage(STORAGE_MANAGER.latestversion);
			if (latestVersion && !latestVersion.islatest) {
				$("#newVersionTip").show();
			} else {
				$("#newVersionTip").hide();
			}
		},
		bindEvent: function() {
			//回退
			$("#back").on("click", function() {
				back();
			});

			$("p[name='accountProfile']").on("click", function() {
				var url = $(this).attr("tip");
				var redirectUrl = "";
				currentLoginUser = getLocalStorage(STORAGE_MANAGER.currentmember);
				isSetPassword = getLocalStorage(STORAGE_MANAGER.issetpaypasswd);
				if (url) {
					//激活优惠券和充值卡
					if (/activateCard.html|activateCoupon.html/.test(url)) {
						//currentLoginUser.phone = '';
						if (!currentLoginUser.is_phone) {
							$.alert("请先绑定手机号", function(e) {
								// + url.replace("../", "").replace("/", "|")
								redirectUrl = "../account/setphone.html?backurl=mine|account_safe.html";
								redirect(redirectUrl);
							});
							return;
						}
						//isSetPassword = false;
						if (!isSetPassword) {
							$.alert("请先设置支付密码", function(e) {
								redirectUrl = "../account/validatePhone.html?type=pay&phone=" + currentLoginUser.phone + "&backurl=" + url.replace("../", "").replace("/", "|");
								redirect(redirectUrl);
							});
							return;
						}
						redirect(url);
					} else {
						redirect(url);
					}
				}
			});

			//清除缓存
			$("#clearCache").on("click", function() {
				$.showWaiting("缓存已清除");
				setTimeout(function() {
					$.closeWaiting();
				}, 1000);
			});

			//退出账号
			$("#logout").on("click", function() {
				clearLocalStorage();
				if (AGENT_CLIENT.APP) {
					plus.webview.getWebviewById("../mine/mylife.html").evalJS("reloadPage()");
					plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(0)");
				} else {
					redirect("../home/index.html");
				}
			});
		}
	};

	thisPage = new account_safePage();
}